﻿using ComLib.ComType;
using ComUIControl;
using CoreBroker;
using EECMISWS;
using HZWebService;
using HZWebService.Models;
using KWindow.KPageCore;
using LogLib;
using Newtonsoft.Json.Linq;
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IPCPages
{
    public partial class PageUploading : PageControlBase
    {   
        private PictureBox pictureBox1 = null;

        private Image imgUploading = null;

        private Image imgUploadFailed = null;

        private Label labelTip = null;

        public PageUploading()
        {          
            InitializeComponent();
            SetProcessBase(ProcessUploading.Instance);
            ProcessUploading p = ProcessUploading.Instance;
            p.UploadFailed += OnUploadFailed;
        }

        private void GetControls()
        {
            pictureBox1 = GetControl<PictureBox>("pictureBox1") as PictureBox;
            imgUploading = GetImage("Uploading");
            imgUploadFailed = GetImage("UploadFailed");
            labelTip = GetControl<Label>("labelTip") as Label;
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();
        }

        /// <summary>
        /// 初始化窗体数据
        /// </summary>
        public override void InitData()
        {
            base.InitData();
            pictureBox1.Image = imgUploading;            
        }
    
        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            base.UpdateData();
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            pictureBox1.Image = null;         
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        private void SetTips(string content)
        {
            labelTip.Text = content;
            int w = labelTip.Width;
            int x = (this.Width - w) / 2;
            labelTip.Left = x;
        }

        public override void StartWork()
        {
            Speak("正在上传照片，请稍候");
            SetTips("正在上传照片，请稍候..."); 
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public void OnUploadFailed(string msg)
        {
            if(this.InvokeRequired)
            {
                Action<string> handler = new Action<string>(OnUploadFailed);
                this.Invoke(handler, new object[] { msg });
                return;
            }
            pictureBox1.Image = imgUploadFailed;
            SetTips(msg);          
            ClearData();
            StartCountdown();
            //Process.ToHome();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            if (Process.Job != null)
            {
                Process.Job.Cancel("倒计时结束，照片上传操作未完成，取消拍照.");
            }
            base.OnCountdownCompleted(sender, e);          
        }
    }
}
